package com.ctrip.study.dp.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ctrip.study.jpa.entity.User;

public class UserServiceHandler implements InvocationHandler{
	
	private Logger logger = LoggerFactory.getLogger(UserServiceHandler.class);
	
	private Object target;
	
	public UserServiceHandler(Object target) {
		this.target = target;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		logger.info("before method");
		Object result = method.invoke(target, args);
		logger.info("after method");
		return result;
	}
	
	public static void main(String[] args) {
		UserServiceImpl target = new UserServiceImpl();
		UserServiceHandler handler = new UserServiceHandler(target);
		UserService proxy = (UserService)Proxy.newProxyInstance(target.getClass().getClassLoader(), 
				target.getClass().getInterfaces(), handler);
		proxy.findUser();
	}
	
}
